package com.bangdream.michelia.view.fragment.main.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.TeacherContract;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.TeacherBean;
import com.bangdream.michelia.presenter.TeacherPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.view.activity.lecturer.LecturerDetails;
import com.bangdream.michelia.view.adapter.EliteAdapter;
import com.bangdream.michelia.view.adapter.EliteLecturerAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @date on 2018/7/19 14:34
 * @email 909732184@qq.com
 * 精英讲师列表
 */
public class EliteLecturerList extends LazyLoadFragment<TeacherContract.ITeacherView,TeacherPresenter<TeacherContract.ITeacherView>>
implements TeacherContract.ITeacherView{
    private View view;
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
//    private EliteLecturerAdapter adapter;
    private EliteAdapter adapter;
    private int page = 1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recycler_view_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
        operateBus();
    }
    private void getData(){
        mPresenter.getTeacherList(page, PublicResources.PageSize,null);
    }
    @Override
    public TeacherPresenter createPresenter() {
        return new TeacherPresenter();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        getData();

    }

    private void setListener() {

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(getActivity(), LecturerDetails.class);
                intent.putExtra("id",adapter.getData().get(position).getId());
                startActivity(intent);
            }
        });

        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                getData();
            }
        });
    }

    private void initView() {
        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView = view.findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new EliteAdapter(R.layout.elitelecturer_adapter_item);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void setTeacherList(boolean isSuccess, int page, List<TeacherBean> data, String stDescribe) {
        refreshView.stopRefresh();
        refreshView.stopLoadMore();

        if (isSuccess && data!=null){
            if(page==1) {
                if(data.size()==0){
                    adapter.setNewData(null);
                    adapter.setEmptyView(noDataView);
                }else {
                    adapter.setNewData(data);
                    adapter.notifyDataSetChanged();
                    //显示数据大于20条才允许上拉加载更多
                    if(data.size()>=PublicResources.PageSize){
                        refreshView.setPullLoadEnable(true);
                    }
                }
            }else {
                int tempIndex = adapter.getData().size();
                for (int i = 0;i<data.size();i++) {
                    adapter.getData().add(data.get(i));
                }
                adapter.notifyItemChanged(tempIndex,adapter.getData().size());
            }
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                adapter.setNewData(null);
                adapter.setEmptyView(isSuccess?noDataView:errorView);
            }
        }
    }

    @Override
    public void setTeacherDetails(boolean isSuccess, TeacherBean data, String stDescribe) {

    }

    @SuppressLint("CheckResult")
    public void operateBus() {
        disposable = RxBus.getInstance().toObservable().map(new Function<Object, RxEventBean>() {
            @Override
            public RxEventBean apply(Object o) throws Exception {
                return (RxEventBean) o;
            }
        }).subscribe(new Consumer<RxEventBean>() {
            @Override
            public void accept(RxEventBean bean) throws Exception {

                if(bean.getCode()==0x02){
                    page=1;
                    refreshView.startRefresh();
                }
            }
        });
    }
}
